import{findCanteenNameById,addSiteIntofoodList} from "../../apis/apis";
import{tapAnimation}from"../../utils/animations";
var app = getApp();
let disableButton=false,sellectingTag=false,disableTagButton=false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    randFoodDetail:{},
    animations:{
      foodCard:{},
      button1:{},
      button2:{},
      selectPanel:{},
      jianTou:{}
    },
    buttonOpacity:1,
    foodTagsArr:[],
    showTags:false,
    tagName:"全部",
    sellectedTag:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFoodTags()
    this.getFoodDetail("");
  },

  //点击"换一个"
  rotate:function(){
    if(!disableButton){
      disableTagButton=true;
      disableButton=true;
      this.setData({
        buttonOpacity:0.6,
      });
      let animation1=wx.createAnimation();
      animation1.scale(0.8).step({duration:600,timingFunction:'ease',delay:200});
      animation1.rotateY(90).scale(0.6).opacity(0).translateZ(100).step({duration:800,timingFunction:'ease'});
      animation1.rotateY(-90).translateZ(-300).step({duration:400,timingFunction:'ease'});
      animation1.rotateY(0).scale(1).translateX(0).opacity(1).step({duration:1200,timingFunction:'ease'});
      this.setData({
        "animations.foodCard":animation1.export(),
        "animations.button2":tapAnimation(),
      });
      setTimeout(()=>{
        this.getFoodDetail(this.data.sellectedTag);
        this.setData({
          buttonOpacity:0.6
        });
      },1400)
      setTimeout(()=>{
        disableButton=false;
        disableTagButton=false;
        this.setData({
          buttonOpacity:1
        });
      },3000)
    }
  },
  
  //获取菜品标签列表
  getFoodTags(){
    wx.request({
      url: app.globalData.apiUrl + '/food/getAllTags',
      method:'GET',
      success:(res)=>{
        console.log(res.data.data)
        this.setData({
          foodTagsArr:res.data.data
        })
      }
    })
  },

  //获取随机菜品信息
  getFoodDetail(tag) {
    wx.request({
      url: app.globalData.apiUrl + '/food/getRandomFoodByTags',
      method: "POST",
      data:{
        "tags":tag
      },
      success: (result) => {
        var newRandFoodDetail= {
          canteenId:result.data.data.data.canteenId,
          comment:result.data.data.data.comment,
          description: result.data.data.data.description,
          foodClassId: result.data.data.data.foodClassId,
          id: result.data.data.data.id,
          imageUrl: result.data.data.data.imageUrl,
          price: result.data.data.data.price,
          tags: result.data.data.data.tags,
          time: result.data.data.data.time,
          title: result.data.data.data.title,
          site:findCanteenNameById(result.data.data.data.canteenId,app.globalData.canteenList)
        }
        this.setData({
          randFoodDetail: newRandFoodDetail
        })
        console.log("随机菜品", this.data.randFoodDetail);
      }
    })
  },

  //跳转菜品详情
  goDetail(){
    wx.navigateTo({
      url: '/pages/foodDetail/foodDetail?id='+this.data.randFoodDetail.id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //初始化选择面板
  initSelectPanel:function(){
    let temp_animation=wx.createAnimation();
    if(sellectingTag){
      temp_animation.height("100rpx").width("180rpx").translate(0).opacity(0).step({timingFunction: 'ease',duration:200});
    }else{
      temp_animation.height("700rpx").width("280rpx").translate("10rpx","-720rpx").opacity(0.98).step({timingFunction: 'ease',duration:200});
    }
    return temp_animation.export()
  },

  //旋转箭头
  rotateJianTou(){
    let temp_animation=wx.createAnimation();
    if(sellectingTag){
      temp_animation.rotate(0).step({timingFunction: 'ease',duration:200});
    }else{
      temp_animation.rotate(-90).step({timingFunction: 'ease',duration:200});
    }
    return temp_animation.export()
  },

  //点击选标签按钮触发
  tapSelectButton:function(){
    if(!disableTagButton){
      this.setData({
        "animations.button1":tapAnimation(),
        "animations.selectPanel":this.initSelectPanel(),
        "animations.jianTou":this.rotateJianTou(),
      })
      if(!this.data.showTags){
        setTimeout(()=>{
          this.setData({
            showTags:true
          })
        },80)
      }else{
        this.setData({
          showTags:false
        })
      }
      sellectingTag=!sellectingTag;
      disableButton=sellectingTag;
    }
  },

  //选中标签
  tapTagItem(options){
    // console.log(options.currentTarget.id)
    this.setData({
      tagName:options.currentTarget.id?options.currentTarget.id:"全部",
      sellectedTag:options.currentTarget.id
    })
  }
})